<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>kaggle项目实战，Titanic analysis | Miki&#39;s blog</title>
  <meta name="keywords" content=" 机器学习 ">
  <meta name="description" content="kaggle项目实战，Titanic analysis | Miki&#39;s blog">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="description" content="keywords: author: Miki language: zh-CN timezone:">
<meta property="og:type" content="website">
<meta property="og:title" content="标签">
<meta property="og:url" content="http://mikiblog.online/tags/index.html">
<meta property="og:site_name" content="Miki&#39;s blog">
<meta property="og:description" content="keywords: author: Miki language: zh-CN timezone:">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-02-22T01:48:02.000Z">
<meta property="article:modified_time" content="2020-02-22T01:50:40.651Z">
<meta property="article:author" content="Miki Zhu">
<meta name="twitter:card" content="summary">


<link rel="icon" href="/img/1.jpg">

<link href="/css/style.css?v=1.0.1" rel="stylesheet">

<link href="/css/hl_theme/atom-dark.css?v=1.0.1" rel="stylesheet">

<link href="//cdn.bootcss.com/animate.css/3.5.2/animate.min.css" rel="stylesheet">
<link href="//cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">

<script src="//cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<script src="/js/jquery.autocomplete.min.js?v=1.0.1" ></script>

<script src="//cdn.bootcss.com/highlight.js/9.12.0/highlight.min.js"></script>
<script>
    hljs.initHighlightingOnLoad();
</script>

<script src="//cdn.bootcss.com/nprogress/0.2.0/nprogress.min.js"></script>



<script src="/js/iconfont.js?v=1.0.1" ></script>

<meta name="generator" content="Hexo 4.2.0"></head>
<div style="display: none">
  <input class="theme_disqus_on" value="false">
  <input class="theme_preload_comment" value="false">
  <input class="theme_blog_path" value="">
</div>

<body>
<aside class="nav">
    <div class="nav-left">
        <a href="/" class="avatar_target">
    <img class="avatar" src="/img/头像.jpg" />
</a>
<div class="author">
    <span>Miki Zhu</span>
</div>

<div class="icon">
    
        
    
        
        <a title="email" href="mailto:747876457@qq.com" target="_blank">
            
                <svg class="iconfont-svg" aria-hidden="true">
                    <use xlink:href="#icon-email"></use>
                </svg>
            
        </a>
        
    
        
        <a title="qq" href="http://wpa.qq.com/msgrd?v=3&uin=747876457&site=qq&menu=yes" target="_blank">
            
                <svg class="iconfont-svg" aria-hidden="true">
                    <use xlink:href="#icon-qq"></use>
                </svg>
            
        </a>
        
    
</div>




<ul>
    <li><div class="all active">全部文章<small>(29)</small></div></li>
    
        
            
            <li><div data-rel="机器学习">机器学习<small>(7)</small></div>
                
            </li>
            
        
    
        
            
            <li><div data-rel="机器学习实战">机器学习实战<small>(3)</small></div>
                
            </li>
            
        
    
        
            
            <li><div data-rel="项目实战">项目实战<small>(4)</small></div>
                
            </li>
            
        
    
        
            
            <li><div data-rel="博客搭建">博客搭建<small>(2)</small></div>
                
            </li>
            
        
    
        
            
            <li><div data-rel="算法">算法<small>(3)</small></div>
                
            </li>
            
        
    
        
            
            <li><div data-rel="python教程">python教程<small>(1)</small></div>
                
            </li>
            
        
    
        
            
            <li><div data-rel="学习网站推荐">学习网站推荐<small>(1)</small></div>
                
            </li>
            
        
    
        
            
            <li><div data-rel="考研经验">考研经验<small>(1)</small></div>
                
            </li>
            
        
    
</ul>
<div class="left-bottom">
    <div class="menus">
    
    
    
    
    </div>
    <div><a class="about  " target="_blank" href="/about">关于</a></div>
</div>
<input type="hidden" id="yelog_site_posts_number" value="29">
<input type="hidden" id="yelog_site_word_count" value="62.1k">
<div style="display: none">
    <span id="busuanzi_value_site_uv"></span>
    <span id="busuanzi_value_site_pv"></span>
</div>

    </div>
    <div class="nav-right">
        <div class="friends-area">
    <div class="friends-title">
        友情链接
        <i class="back-title-list"></i>
    </div>
    <div class="friends-content">
        <ul>
            
        </ul>
    </div>
</div>
        <div class="title-list">
    <form onkeydown="if(event.keyCode === 13){return false;}">
        <input id="local-search-input" class="search" type="text" placeholder="Search..." />
        <i class="cross"></i>
        <span>
            <label for="tagswitch">Tags:</label>
            <input id="tagswitch" type="checkbox" style="display: none" />
            <i id="tagsWitchIcon"></i>
        </span>
    </form>
    <div class="tags-list">
    
    <li class="article-tag-list-item">
        <a class="color5">机器学习</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color3">算法</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color5">博客搭建</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color2">python</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color5">网站推荐</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color5">考研经验</a>
    </li>
    
    <div class="clearfix"></div>
</div>

    
    <nav id="title-list-nav">
        
        <a id="top" class="python教程 "
           href="/2020/04/03/python%E9%9D%A2%E8%AF%95%E9%A2%98/"
           data-tag="python"
           data-author="" >
            <span class="post-title" title="python面试题(不定期更新)">python面试题(不定期更新)</span>
            <span class="post-date" title="2020-04-03 21:14:43">2020/04/03</span>
        </a>
        
        <a id="top" class="考研经验 "
           href="/2020/02/20/%E8%80%83%E7%A0%94%E5%88%9D%E8%AF%95%E7%BB%8F%E9%AA%8C/"
           data-tag="考研经验"
           data-author="" >
            <span class="post-title" title="考研初试经验">考研初试经验</span>
            <span class="post-date" title="2020-02-20 19:33:48">2020/02/20</span>
        </a>
        
        <a  class="机器学习实战 "
           href="/2020/04/19/%E9%80%BB%E8%BE%91%E5%9B%9E%E5%BD%92%E4%BB%A3%E7%A0%81%E5%AE%9E%E7%8E%B0/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="逻辑回归代码实现">逻辑回归代码实现</span>
            <span class="post-date" title="2020-04-19 21:09:25">2020/04/19</span>
        </a>
        
        <a  class="机器学习 "
           href="/2020/04/14/%E9%A9%AC%E5%B0%94%E5%8F%AF%E5%A4%AB%E9%93%BE/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="马尔可夫链">马尔可夫链</span>
            <span class="post-date" title="2020-04-14 19:09:29">2020/04/14</span>
        </a>
        
        <a  class="机器学习实战 "
           href="/2020/04/14/%E5%86%B3%E7%AD%96%E6%A0%91%E5%AE%9E%E6%88%98%E9%9A%90%E5%BD%A2%E7%9C%BC%E9%95%9C%E5%88%86%E7%B1%BB/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="决策树实战隐形眼镜分类">决策树实战隐形眼镜分类</span>
            <span class="post-date" title="2020-04-14 10:49:40">2020/04/14</span>
        </a>
        
        <a  class="机器学习实战 "
           href="/2020/04/12/KNN%E5%AE%9E%E6%88%98%E4%B9%8B%E6%B5%B7%E4%BC%A6%E7%BA%A6%E4%BC%9A/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="KNN实战之海伦约会">KNN实战之海伦约会</span>
            <span class="post-date" title="2020-04-12 08:22:12">2020/04/12</span>
        </a>
        
        <a  class="博客搭建 "
           href="/2020/04/08/hexo-%E4%B8%BB%E9%A2%98%E6%B7%BB%E5%8A%A0%E8%AF%84%E8%AE%BA/"
           data-tag="博客搭建"
           data-author="" >
            <span class="post-title" title="hexo 主题添加评论">hexo 主题添加评论</span>
            <span class="post-date" title="2020-04-08 21:43:26">2020/04/08</span>
        </a>
        
        <a  class="项目实战 "
           href="/2020/03/30/%E6%98%8E%E5%A4%A9%E4%BC%9A%E4%B8%8B%E9%9B%A8%E5%90%97%EF%BC%9F/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="明天会下雨吗？">明天会下雨吗？</span>
            <span class="post-date" title="2020-03-30 11:25:31">2020/03/30</span>
        </a>
        
        <a  class="项目实战 "
           href="/2020/03/23/%E5%86%B3%E7%AD%96%E6%A0%91%E5%AE%9E%E7%8E%B0Titanic-%E9%87%8D%E8%A6%81/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="决策树实现Titanic(重要)">决策树实现Titanic(重要)</span>
            <span class="post-date" title="2020-03-23 17:09:05">2020/03/23</span>
        </a>
        
        <a  class="项目实战 "
           href="/2020/03/21/Titanic-%E9%A1%B9%E7%9B%AE%E8%AF%A6%E8%A7%A3/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="Titanic 项目详解">Titanic 项目详解</span>
            <span class="post-date" title="2020-03-21 14:07:20">2020/03/21</span>
        </a>
        
        <a  class="项目实战 "
           href="/2020/03/19/kaggle%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="kaggle项目实战，Titanic analysis">kaggle项目实战，Titanic analysis</span>
            <span class="post-date" title="2020-03-19 10:08:13">2020/03/19</span>
        </a>
        
        <a  class=""
           href="/2020/03/17/Pytorch-%E6%90%AD%E5%BB%BA%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="Pytorch 搭建神经网络">Pytorch 搭建神经网络</span>
            <span class="post-date" title="2020-03-17 14:56:16">2020/03/17</span>
        </a>
        
        <a  class=""
           href="/2020/03/16/CNN%E5%8E%9F%E7%90%86/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="CNN">CNN</span>
            <span class="post-date" title="2020-03-16 09:11:00">2020/03/16</span>
        </a>
        
        <a  class=""
           href="/2020/03/14/%E9%9A%8F%E6%9C%BA%E6%A3%AE%E6%9E%97%E5%92%8Ck%E5%9D%87%E5%80%BC/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="random forest and k means">random forest and k means</span>
            <span class="post-date" title="2020-03-14 16:29:22">2020/03/14</span>
        </a>
        
        <a  class=""
           href="/2020/03/13/%E6%94%AF%E6%8C%81%E5%90%91%E9%87%8F%E6%9C%BA%E7%AE%97%E6%B3%95/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="支持向量机">支持向量机</span>
            <span class="post-date" title="2020-03-13 20:53:20">2020/03/13</span>
        </a>
        
        <a  class=""
           href="/2020/03/11/Matplotlab/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="Matplotlab">Matplotlab</span>
            <span class="post-date" title="2020-03-11 10:13:20">2020/03/11</span>
        </a>
        
        <a  class=""
           href="/2020/03/10/pandas/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="pandas">pandas</span>
            <span class="post-date" title="2020-03-10 09:20:27">2020/03/10</span>
        </a>
        
        <a  class=""
           href="/2020/03/09/logical-regression/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="logical regression">logical regression</span>
            <span class="post-date" title="2020-03-09 20:30:33">2020/03/09</span>
        </a>
        
        <a  class="学习网站推荐 "
           href="/2020/03/08/%E5%AD%A6%E4%B9%A0%E7%BD%91%E7%AB%99%E6%88%96%E5%B7%A5%E5%85%B7%E6%8E%A8%E8%8D%90/"
           data-tag="网站推荐"
           data-author="" >
            <span class="post-title" title="学习网站或工具推荐">学习网站或工具推荐</span>
            <span class="post-date" title="2020-03-08 20:44:55">2020/03/08</span>
        </a>
        
        <a  class="机器学习 "
           href="/2020/03/08/%E8%B4%9D%E5%8F%B6%E6%96%AF%E5%88%86%E6%9E%90/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="贝叶斯分析">贝叶斯分析</span>
            <span class="post-date" title="2020-03-08 20:11:15">2020/03/08</span>
        </a>
        
        <a  class="机器学习 "
           href="/2020/03/08/numpy/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="numpy的使用">numpy的使用</span>
            <span class="post-date" title="2020-03-08 19:15:54">2020/03/08</span>
        </a>
        
        <a  class="机器学习 "
           href="/2020/03/07/%E5%86%B3%E7%AD%96%E6%A0%91/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="决策树">决策树</span>
            <span class="post-date" title="2020-03-07 19:27:38">2020/03/07</span>
        </a>
        
        <a  class="机器学习 "
           href="/2020/03/06/K-%E8%BF%91%E9%82%BB%E7%AE%97%E6%B3%95/"
           data-tag="算法"
           data-author="" >
            <span class="post-title" title="K-近邻算法">K-近邻算法</span>
            <span class="post-date" title="2020-03-06 20:18:19">2020/03/06</span>
        </a>
        
        <a  class="算法 "
           href="/2020/03/05/%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95/"
           data-tag="算法"
           data-author="" >
            <span class="post-title" title="排序算法">排序算法</span>
            <span class="post-date" title="2020-03-05 19:41:32">2020/03/05</span>
        </a>
        
        <a  class="算法 "
           href="/2020/03/04/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E6%97%B6%E9%97%B4%E5%A4%8D%E6%9D%82%E5%BA%A6/"
           data-tag="算法"
           data-author="" >
            <span class="post-title" title="数据结构与时间复杂度">数据结构与时间复杂度</span>
            <span class="post-date" title="2020-03-04 21:00:32">2020/03/04</span>
        </a>
        
        <a  class="机器学习 "
           href="/2020/03/04/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E6%95%B0%E5%AD%A6%E5%9F%BA%E7%A1%80/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="机器学习数学基础">机器学习数学基础</span>
            <span class="post-date" title="2020-03-04 16:48:34">2020/03/04</span>
        </a>
        
        <a  class="算法 "
           href="/2020/03/03/leetcode%E7%AE%97%E6%B3%95%E7%BB%83%E4%B9%A0/"
           data-tag="算法"
           data-author="" >
            <span class="post-title" title="哈希图 and 移动窗口">哈希图 and 移动窗口</span>
            <span class="post-date" title="2020-03-03 18:49:52">2020/03/03</span>
        </a>
        
        <a  class="机器学习 "
           href="/2020/03/01/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E5%9F%BA%E6%9C%AC%E6%9C%AF%E8%AF%AD/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="1 零基础学机器学习">1 零基础学机器学习</span>
            <span class="post-date" title="2020-03-01 20:23:40">2020/03/01</span>
        </a>
        
        <a  class="博客搭建 "
           href="/2020/02/20/Windows%E7%B3%BB%E7%BB%9F%E4%B8%8B%E7%94%A8hexo%E6%90%AD%E5%BB%BA%E4%B8%AA%E4%BA%BA%E5%8D%9A%E5%AE%A2/"
           data-tag="博客搭建"
           data-author="" >
            <span class="post-title" title="Windows系统下用hexo搭建个人博客">Windows系统下用hexo搭建个人博客</span>
            <span class="post-date" title="2020-02-20 13:15:25">2020/02/20</span>
        </a>
        
    </nav>
</div>
    </div>
    <div class="hide-list">
        <div class="semicircle">
            <div class="brackets first"><</div>
            <div class="brackets">&gt;</div>
        </div>
    </div>
</aside>
<div class="post">
    <div class="pjax">
        <article id="post-kaggle项目实战" class="article article-type-post" itemscope itemprop="blogPost">
    
        <h1 class="article-title">kaggle项目实战，Titanic analysis</h1>
    
    <div class="article-meta">
        
        
        
        <span class="book">
            
                <a  data-rel="项目实战">项目实战</a>
            
        </span>
        
        
        <span class="tag">
            
            <a class="color5">机器学习</a>
            
        </span>
        
    </div>
    <div class="article-meta">
        
        创建时间:<time class="date" title='更新时间: 2020-03-22 20:33:21'>2020-03-19 10:08</time>
        
    </div>
    <div class="article-meta">
        
        <span>字数:4.1k</span>
        
        
        <span id="busuanzi_container_page_pv">
            阅读:<span id="busuanzi_value_page_pv">
                <span class="count-comment">
                    <span class="spinner">
                      <div class="cube1"></div>
                      <div class="cube2"></div>
                    </span>
                </span>
            </span>
        </span>
        
        
        <span class="top-comment" title="跳转至评论区">
            <a href="#comments">
                评论:<span class="count-comment">
                    <span class="spinner">
                      <div class="cube1"></div>
                      <div class="cube2"></div>
                    </span>
                </span>
            </a>
        </span>
        
    </div>
    
    <div class="toc-ref">
    
        <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#问题分析"><span class="toc-text"> 问题分析</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#数据预处理"><span class="toc-text"> 数据预处理</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#对缺失数据的填充"><span class="toc-text"> 对缺失数据的填充</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#将字符串类型数据转换成整型或浮点型"><span class="toc-text"> 将字符串类型数据转换成整型或浮点型</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#交叉验证"><span class="toc-text"> 交叉验证</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#选择算法"><span class="toc-text"> 选择算法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#多元线性回归模型"><span class="toc-text"> 多元线性回归模型</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#模型改进1"><span class="toc-text"> 模型改进1</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#逻辑回归算法"><span class="toc-text"> 逻辑回归算法</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#模型改进2"><span class="toc-text"> 模型改进2</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#随机森林算法"><span class="toc-text"> 随机森林算法</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#特征提取与模型的进一步改进重要"><span class="toc-text"> 特征提取与模型的进一步改进(重要)</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#判断特征的重要程度特征选择重要"><span class="toc-text"> 判断特征的重要程度(特征选择)(重要！！！)</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#pandas数据归一化"><span class="toc-text"> pandas数据归一化</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#完整程序"><span class="toc-text"> 完整程序</span></a></li></ol>
    
<style>
    .left-col .switch-btn,
    .left-col .switch-area {
        display: none;
    }
    .toc-level-3 i,
    .toc-level-3 ol {
        display: none !important;
    }
</style>
</div>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="问题分析"><a class="markdownIt-Anchor" href="#问题分析"></a> 问题分析</h2>
<blockquote>
<p>In this challenge, we ask you to build a predictive model that answers the question: “what sorts of people were more likely to survive?” using passenger data (ie name, age, gender, socio-economic class, etc).</p>
</blockquote>
<p>泰坦尼克号灾难发生时，死亡了很多人，但也有一部分人活了下来，我们将提供一些有标签的数据集，请你根据这些数据对建立的模型进行训练，然后再测试没标签的数据集，判断一个人是否会存活下来，选择合适的算法，提高分类的准确率</p>
<ul>
<li>典型的二分类问题，根据年龄、性别等特征，判断一个人是死亡（0），还是存活（1）</li>
</ul>
<h2 id="数据预处理"><a class="markdownIt-Anchor" href="#数据预处理"></a> 数据预处理</h2>
<pre class="highlight"><code class="">train_data = pd.read_csv(r&quot;C:\Users\asus\Desktop\train.csv&quot;)
# print(train_data.head())##打印前几行数据
print(train_data.describe().loc[:,[&quot;Survived&quot;,&quot;Age&quot;]])##对数据进行统计，按列(特征)进行统计
</code></pre>
<ul>
<li>读取文件，用<code>head(3)</code>方法打印前进行数据，观察数据的内容</li>
<li>使用<code>describe()</code>方法，对数据的每一列进行统计，统计的内容有（行数，平均值，方差，最小值，最大值）</li>
<li>使用<code>loc()</code>方法，对数据进行切片，数据量太大会显示不出来</li>
</ul>
<pre class="highlight"><code class="">		Survived         Age
count  891.000000  714.000000
mean     0.383838   29.699118
std      0.486592   14.526497
min      0.000000    0.420000
25%      0.000000   20.125000
50%      0.000000   28.000000
75%      1.000000   38.000000
max      1.000000   80.000000
</code></pre>
<pre class="highlight"><code class="">       PassengerId    Survived      Pclass  ...       SibSp       Parch        Fare
count   891.000000  891.000000  891.000000  ...  891.000000  891.000000  891.000000
mean    446.000000    0.383838    2.308642  ...    0.523008    0.381594   32.204208
std     257.353842    0.486592    0.836071  ...    1.102743    0.806057   49.693429
</code></pre>
<p><font color='red'> 我们可以看到，直接<code>describe()</code>的话，数据是显示不完全的，当我们对数据进行统计时，发现年龄这一栏的数据，有些缺失了</font></p>
<h3 id="对缺失数据的填充"><a class="markdownIt-Anchor" href="#对缺失数据的填充"></a> 对缺失数据的填充</h3>
<pre class="highlight"><code class="">train_data['Age'] = train_data['Age'].fillna(train_data['Age'].median())
print(train_data['Age'].median())
</code></pre>
<ol>
<li>pandas可以按行索引也可以按标签进行索引，这里按标签进行索引</li>
<li>由于<code>Age</code>这一列数据有缺失，我们必须将数据进行填补，选取平均值进行填充，使用<code>fillna()</code>方法和<code>median()</code>方法</li>
<li><code>train_data['Age'] = train_data['Age'].fillna(train_data['Age'].median())</code>,对<code>Age</code>列计算平均值并进行填充，再进行赋值**(等号右边操作不会影响原来的列)**</li>
</ol>
<pre class="highlight"><code class="">print(train_data.describe().loc[:, ['Survived', 'Age']])
</code></pre>
<pre class="highlight"><code class="">         Survived         Age
count  891.000000  891.000000
mean     0.383838   29.361582
std      0.486592   13.019697
</code></pre>
<ul>
<li>填充完以后的效果</li>
</ul>
<hr />
<pre class="highlight"><code class="">from collections import Counter
print(Counter(train.Embarked))
</code></pre>
<pre class="highlight"><code class="">Counter({'S': 644, 'C': 168, 'Q': 77, nan: 2})
</code></pre>
<p><font color='red'>我们还可以看到，登船这一数据，有两个值是缺失的，我们需要用S补上，因为S是最多的，所以这两个空值是S的概率也就越高</font></p>
<pre class="highlight"><code class="">train['Embarked'] = train['Embarked'].fillna(&quot;S&quot;)
train.loc[train.Embarked == 'S',['Embarked']], train.loc[train.Embarked == 'C',['Embarked']], \
train.loc[train.Embarked == 'Q',['Embarked']] = 0, 1, 2
</code></pre>
<ul>
<li>数据处理完毕</li>
</ul>
<hr />
<h3 id="将字符串类型数据转换成整型或浮点型"><a class="markdownIt-Anchor" href="#将字符串类型数据转换成整型或浮点型"></a> 将字符串类型数据转换成整型或浮点型</h3>
<pre class="highlight"><code class="">print(train_data['Sex'].head())
</code></pre>
<pre class="highlight"><code class="">0      male
1    female
2    female
3    female
4      male
</code></pre>
<p><strong>我们可以发现性别这一栏的数据都是字符串类型的，所以我们需要将字符串转换成浮点型或者整型</strong></p>
<blockquote>
<p>将女性标记为0，男性标记为1</p>
</blockquote>
<ul>
<li>思路：用pandas中的布尔索引法，对数据进行修改</li>
</ul>
<pre class="highlight"><code class="">train.loc[train.Sex == &quot;male&quot;, ['Sex']], train.loc[train.Sex == &quot;female&quot;, ['Sex']] = 1,0
</code></pre>
<pre class="highlight"><code class="">0      1
1      0
2      0
3      0
4      1
</code></pre>
<h2 id="交叉验证"><a class="markdownIt-Anchor" href="#交叉验证"></a> 交叉验证</h2>
<ul>
<li>先介绍下什么是交叉验证</li>
</ul>
<p>叉验证的基本思想是重复地使用数据；把给定的数据进行切分，将切分的数据组合为训练集和测试集，在此基础上反复地进行训练、测试以及模型选择。</p>
<p>简单交叉验证是：首先随机地将已给数据分成两部分，一部分作为训练集，另一部分作为测试集（比如，70%的数据为训练集，30%的数据为测试集）；然后用训练集在各种情况下（例如，不同的参数个数）训练模型，从而得到不同的模型；在测试集上评价各个模型的测试误差，选出测试误差最小的模型。</p>
<p><img src="https://scikit-learn.org/stable/_images/sphx_glr_plot_cv_indices_0041.png" alt="../_images/sphx_glr_plot_cv_indices_0041.png" /></p>
<p>我们可以发现，每个数据都会被用到测试，也会被用到进行训练</p>
<pre class="highlight"><code class="">&gt;&gt;&gt; X = [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;]
&gt;&gt;&gt; kf = KFold(n_splits=2)
&gt;&gt;&gt; for train, test in kf.split(X):
...     print(&quot;%s %s&quot; % (train, test))
[2 3] [0 1]
[0 1] [2 3]
</code></pre>
<h2 id="选择算法"><a class="markdownIt-Anchor" href="#选择算法"></a> 选择算法</h2>
<h3 id="多元线性回归模型"><a class="markdownIt-Anchor" href="#多元线性回归模型"></a> 多元线性回归模型</h3>
<p>当特征量比较多时，我们可以建立多元线性回归模型，来对数据进行预测，虽然效果可能不好，但是我们可以先用这个方法进行测试</p>
<pre class="highlight"><code class="">algorithm = LinearRegression()
</code></pre>
<p>使用k折交叉分类方法，将数据进行拆分，分成</p>
<pre class="highlight"><code class="">kf = KFold(n_splits=3, shuffle=True, random_state=1)
predictions = []
for each_train, test in kf.split(train):
</code></pre>
<p>可以看到，拆分只是挑选了数据的序号</p>
<pre class="highlight"><code class="">[  0   3   4   6   7   9  11  13  15  16  19  21  22  23  24  25  26  28
  29  32  33  35  36  38  41  42  43  44  46  47  51  53  56  57  58  59
  61  63  66  67  69  70  72  73  74  78  80  81  82  83  84  86  87  88···]
</code></pre>
<ul>
<li><code>n_splits=3</code>表示数据会分三次，也就是说，先将数据分成两部分，一部分用来训练，一部分用来测试，第二次分又会随机分，再挑一部分作为训练，一部分用来测试，一共分三次</li>
</ul>
<p>打印一下循环中的那些代码：（用来进行测试的）</p>
<p>先选取这些特征标签，定位到他们测试集的数据</p>
<pre class="highlight"><code class="">predictors = ['Pclass', 'Sex', 'Age', 'SibSp','Parch', 'Fare', 'Embarked']
print(train[predictors].iloc[each_train, :].head())
</code></pre>
<pre class="highlight"><code class="">   Pclass  Sex   Age  SibSp  Parch     Fare  Embarked
1       1    1  38.0      1      0  71.2833         1
4       3    0  35.0      0      0   8.0500         0
5       3    0  28.0      0      0   8.4583         2
7       3    0   2.0      3      1  21.0750         0
9       2    1  14.0      1      0  30.0708         1
</code></pre>
<ol>
<li>首先进行训练，挑选出特征和结果(标签)，进行分析，也就是训练好下面的系数</li>
<li>挑选出待预测的，作为新的输入，每输入一个就会得到一个输出，我们发现下面这个输出几乎都是在0-1区间的</li>
</ol>
<p><img src="https://img-blog.csdnimg.cn/20181119004628363.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2tlcGVuZ3M=,size_16,color_FFFFFF,t_70" alt="img" /></p>
<pre class="highlight"><code class="">train_data = (train[predictors].iloc[each_train, :])
train_target = (train['Survived'].iloc[each_train])
algorithm.fit(train_data, train_target)
test_pre = algorithm.predict(train[predictors].iloc[test, :])
print(test_pre)
</code></pre>
<pre class="highlight"><code class="">[ 0.06437899  0.61788304  0.87297606  0.29033014  0.64169133  0.03664443
  0.69135238  0.12120566  0.26493885  0.64810848  0.4479658   0.43909047
  0.30794885  0.47643842  0.73801641  0.1135054   0.69069692  0.61193961
</code></pre>
<p>由于这里预测的是数组，我们需要用到<code>extend</code>方法，和<code>append</code>方法有什么区别可以自己试试</p>
<pre class="highlight"><code class="">count = 0
for key, values in enumerate(tag):
    if predictions[key] &gt;= 0.5:
        predictions[key] = 1
    else:
        predictions[key] = 0
    if predictions[key] != train.Survived[values]:
        count += 1
print(f'rate={1 - (count/len(train))}')
</code></pre>
<ul>
<li>这里我们让大于0.5的认定为存活下来，小于0.5的则没存活</li>
</ul>
<pre class="highlight"><code class="">rate=0.7901234567901234
</code></pre>
<ul>
<li><strong>结果准确率只有0.79，看来是不算太高的</strong></li>
</ul>
<p><strong>这里先大致讲一下思路：</strong></p>
<pre class="highlight"><code class="">kf = KFold(n_splits=2, shuffle=True, random_state=1)
for each_train, test in kf.split(train):
	print(each_train)
    print(test)
</code></pre>
<pre class="highlight"><code class="">1   7  10  15
 0   2   3   4   
 0   2   3   4   
 1   7  10  15
</code></pre>
<ul>
<li>由此可见分成了两部分，每一个数据都会被测试，也都会被用来当作训练</li>
</ul>
<pre class="highlight"><code class="">for each_train, test in kf.split(train):
    train_data = (train[predictors].iloc[each_train, :])
    train_target = (train['Survived'].iloc[each_train])
    algorithm.fit(train_data, train_target)
    test_pre = algorithm.predict(train[predictors].iloc[test, :])
    predictions.extend(test_pre)
    tag.extend(test)
</code></pre>
<ul>
<li><code>fit(train_data, train_target)</code>这一步是进行数据的训练</li>
<li><code>predict(train[predictors].iloc[test, :])数据的预测</code>，输出是一系列介于0-1之间的小数</li>
<li><code>predictions.extend(test_pre)</code> <code>tag.extend(test)</code>由于数据是打乱的，所以要将标签搜集起来</li>
</ul>
<p><strong>完整代码如下：</strong></p>
<pre class="highlight"><code class="">import pandas as pd
from collections import Counter
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import KFold

train = pd.read_csv(r&quot;C:\Users\asus\Desktop\train.csv&quot;)
# print(train.head())##打印前几行数据
# print(train.describe().loc['Survived', 'Age'])##对数据进行统计，按列(特征)进行统计
train['Age'] = train['Age'].fillna(train['Age'].median())
# print(train.describe().loc[:, ['Survived', 'Age']])
# 这里使用loc方法，Sex列的所有满足条件的
train.loc[train.Sex == &quot;male&quot;, ['Sex']], train.loc[train.Sex == &quot;female&quot;, ['Sex']] = 0, 1
# print(Counter(train.Embarked))
train['Embarked'] = train['Embarked'].fillna(&quot;S&quot;)
train.loc[train.Embarked == 'S',['Embarked']], train.loc[train.Embarked == 'C',['Embarked']], \
train.loc[train.Embarked == 'Q',['Embarked']] = 0, 1, 2
# print(Counter(train.Embarked))
# print(train.Embarked.describe())
predictors = ['Pclass', 'Sex', 'Age', 'SibSp','Parch', 'Fare', 'Embarked']
# print(train.columns) 打印标签
algorithm = LinearRegression()
kf = KFold(n_splits=2, shuffle=True, random_state=1)
predictions = []
tag = []
for each_train, test in kf.split(train):
    train_data = (train[predictors].iloc[each_train, :])
    train_target = (train['Survived'].iloc[each_train])
    algorithm.fit(train_data, train_target)
    test_pre = algorithm.predict(train[predictors].iloc[test, :])
    predictions.extend(test_pre)
    tag.extend(test)
count = 0
for key, values in enumerate(tag):
    if predictions[key] &gt;= 0.5:
        predictions[key] = 1
    else:
        predictions[key] = 0
    if predictions[key] != train.Survived[values]:
        count += 1
print(f'rate={1 - (count/len(train))}')
</code></pre>
<blockquote>
<p><strong>准确率只有0.79</strong></p>
<p>如果我将&gt;0.6视为存活的话，准确率将达到百分之八十</p>
</blockquote>
<h2 id="模型改进1"><a class="markdownIt-Anchor" href="#模型改进1"></a> 模型改进1</h2>
<h3 id="逻辑回归算法"><a class="markdownIt-Anchor" href="#逻辑回归算法"></a> 逻辑回归算法</h3>
<p><code>rate=0.797979797979798</code></p>
<p>代码改进如下：</p>
<p><code>from sklearn.linear_model import LogisticRegression</code></p>
<p><code>algorithm = LogisticRegression(solver='liblinear')</code></p>
<pre class="highlight"><code class="">count = 0
for key, values in enumerate(tag):
    if predictions[key] != train.Survived[values]:
        count += 1
</code></pre>
<ul>
<li>总的来说就是不需要讨论&gt; 0.5就是1了，不需要对数据进行处理了，逻辑回归算法就是对数据进行分类的，而线性回归预测的值是连续的</li>
<li>只要对原先的代码进行上诉三点改进即可</li>
</ul>
<h2 id="模型改进2"><a class="markdownIt-Anchor" href="#模型改进2"></a> 模型改进2</h2>
<h3 id="随机森林算法"><a class="markdownIt-Anchor" href="#随机森林算法"></a> 随机森林算法</h3>
<blockquote>
<p>随机森林是由多颗决策树组合而成的，假如有三颗决策树，分别输出结果是011，那么最后采取投票的方式，认定结果是1</p>
<p>随机森林的随机体现在：</p>
<ul>
<li>抽样随机，样本是有放回地抽取</li>
<li>特征选取随机，比如从特征中选取年龄，性别等特征，判断哪个特征影响比较大</li>
</ul>
</blockquote>
<p>代码改进：</p>
<pre class="highlight"><code class="">from sklearn.ensemble import RandomForestClassifier
algorithm = RandomForestClassifier(random_state=1, n_estimators=80, min_samples_split=2, min_samples_leaf=1)
</code></pre>
<p><strong>参数说明：</strong></p>
<ul>
<li><code>random_state=1</code>表示每次随机都会得到一样的结果，如果不设置这个值，每次程序运行都会得到不一样的值</li>
<li><code>n_estimators=80</code>表示决策树的数量，越多准确率会相对于更高</li>
<li><code>min_samples_split=2, min_samples_leaf=1</code>，停止条件
<ul>
<li><code>min_samples_split=2</code>表示最小分的数量为2，则停止分裂</li>
<li><code>min_samples_leaf=1</code>当叶子结点为1时，停止分裂</li>
</ul>
</li>
</ul>
<pre class="highlight"><code class="">algorithm = RandomForestClassifier(random_state=1, n_estimators=80, min_samples_split=4, min_samples_leaf=2)
</code></pre>
<p><strong>如果对参数继续进行修改</strong>，使得决策树的层数更深，则准确率将会进一步提升</p>
<pre class="highlight"><code class="">rate=0.8249158249158249
</code></pre>
<ol>
<li><strong>n_estimators</strong>: 也就是最大的弱学习器的个数。一般来说n_estimators太小，容易欠拟合，n_estimators太大，计算量会太大，并且n_estimators到一定的数量后，再增大n_estimators获得的模型提升会很小，所以一般选择一个适中的数值。默认是100。</li>
</ol>
<p><a href="https://www.cnblogs.com/pinard/p/6160412.html?utm_source=itdadao&amp;utm_medium=referral" target="_blank" rel="noopener">随机森林参数详解</a></p>
<h2 id="特征提取与模型的进一步改进重要"><a class="markdownIt-Anchor" href="#特征提取与模型的进一步改进重要"></a> 特征提取与模型的进一步改进(重要)</h2>
<table>
<thead>
<tr>
<th>Pclass</th>
<th>Name</th>
<th>Sex</th>
<th>Age</th>
<th>SibSp</th>
<th>Parch</th>
<th>Ticket</th>
<th>Fare</th>
<th>Cabin</th>
<th>Embarked</th>
</tr>
</thead>
<tbody>
<tr>
<td>3</td>
<td>Braund, Mr. Owen Harris</td>
<td>male</td>
<td>22</td>
<td>1</td>
<td>0</td>
<td>A/5 21171</td>
<td>7.25</td>
<td></td>
<td>S</td>
</tr>
<tr>
<td>1</td>
<td>Cumings, Mrs. John Bradley (Florence Briggs Thayer)</td>
<td>female</td>
<td>38</td>
<td>1</td>
<td>0</td>
<td>PC 17599</td>
<td>71.2833</td>
<td>C85</td>
<td>C</td>
</tr>
</tbody>
</table>
<p><strong>在前面的模型中，我们并没有考虑到姓名，家庭成员数对生存的影响</strong></p>
<p>对模型进行进一步假设：</p>
<ul>
<li>在美国，似乎名字越长，身份也就越重要，因此可以尝试把<strong>姓名长度</strong>，<strong>作为一个新的特征</strong></li>
<li><code>Cumings, Mrs. John Bradley (Florence Briggs Thayer)</code>,随便观察一个名字，我们可以发现其中藏着一个人的身份，因此可以对所有名字进行一个统计<code>Mrs</code>,作为一个新特征</li>
<li>家庭成员数，似乎对能否生存也有一定的影响</li>
</ul>
<pre class="highlight"><code class="">import pandas as pd
import re

train = pd.read_csv(r'C:\Users\asus\Desktop\train.csv')
r = re.compile(r', (.*?). ', re.S)
length_of_name = []
name_tag = {}
for each_name in train.Name:
    length_of_name.append(len(each_name))
    tag = re.search(r, each_name).group(1)
    if tag not in name_tag:
        name_tag[tag] = 0
    name_tag[tag] += 1
</code></pre>
<pre class="highlight"><code class="">{'Mr': 517, 'Mrs': 125, 'Miss': 182
</code></pre>
<ul>
<li>正则表达式中<code>group()</code>会将正则中用<code>()</code>括起来的直接输出</li>
</ul>
<pre class="highlight"><code class="">import pandas as pd
import re

train = pd.read_csv(r'C:\Users\asus\Desktop\train.csv')
#使用正则对姓名进行处理
r = re.compile(r', (.*?). ', re.S)
length_of_name = []
name_tag = {}
name =[]
for each_name in train.Name:
    length_of_name.append(len(each_name))
    tag = re.search(r, each_name).group(1)
    name.append(tag)
    if tag not in name_tag:
        name_tag[tag] = 0
    name_tag[tag] += 1
#zip直接输出是一个迭代器类型，要转换成字典
tag_map = dict(zip(name_tag.keys(), [i for i in range(1, len(name_tag) + 1)]))
name_tag_digit = []
for each in name:
    name_tag_digit.append(tag_map[each])
#加上新特征
train['length of name'] = length_of_name
train['name tag'] = name_tag_digit
print(train.head())
</code></pre>
<ul>
<li>这一段代码计算了名字的长度新标签，设定了身份代码，<code>Mr=1，Mrs=2...</code></li>
</ul>
<pre class="highlight"><code class="">   PassengerId  Survived  Pclass  ... Embarked length of name  name tag
0            1         0       3  ...        S             23         1
1            2         1       1  ...        C             51         2
</code></pre>
<h2 id="判断特征的重要程度特征选择重要"><a class="markdownIt-Anchor" href="#判断特征的重要程度特征选择重要"></a> 判断特征的重要程度(特征选择)(重要！！！)</h2>
<p>判断某个参数对是否生存的影响程度</p>
<p><a href="https://github.com/fuqiuai/sklearn-feature-engineering" target="_blank" rel="noopener">参考Github，特征选择</a>，介绍了数据映射到0-1区间的方法，归一化处理</p>
<blockquote>
<p>特征选择就是先单独拿出某一项特征，对数据进行拟合，得到一个错误率1，当我们向这个特征中添加某些数据时，再进行拟合，会得到错误率2，如果错误率上升的越厉害，则说明这个特征影响能力越大</p>
<pre class="highlight"><code class="">
</code></pre>
</blockquote>
<p><a href="https://blog.csdn.net/baidu_37995814/article/details/98727766" target="_blank" rel="noopener">当matplotlib画图文字遮挡怎么处理</a></p>
<p><img src="http://q6ip4it64.bkt.clouddn.com/%E7%89%B9%E5%BE%81.png?e=1584711672&amp;token=IeqxMYJS9TcEnX8V6lUXD9FF_y3SCdOBApPAMpRy:olQmhX0_b8dbAi2bEz4kgpoANFY=&amp;attname=" alt="http://q6ip4it64.bkt.clouddn.com/%E7%89%B9%E5%BE%81.png?e=1584711672&amp;token=IeqxMYJS9TcEnX8V6lUXD9FF_y3SCdOBApPAMpRy:olQmhX0_b8dbAi2bEz4kgpoANFY=&amp;attname=" /></p>
<ul>
<li>从图中可以看出这些参数的影响程度</li>
</ul>
<h2 id="pandas数据归一化"><a class="markdownIt-Anchor" href="#pandas数据归一化"></a> pandas数据归一化</h2>
<pre class="highlight"><code class="">#归一化
norm_data =(train[predictors] - train[predictors].min()) / (train[predictors].max() - train[predictors].min())
</code></pre>
<h2 id="完整程序"><a class="markdownIt-Anchor" href="#完整程序"></a> 完整程序</h2>
<pre class="highlight"><code class="">import pandas as pd
import re
import numpy as np
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import f_classif
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import KFold

train = pd.read_csv(r'C:\Users\asus\Desktop\train.csv')
r = re.compile(r', (.*?). ', re.S)
length_of_name = []
name_tag = {}
name = []

train['Age'] = train['Age'].fillna(train['Age'].median())
# print(train.describe().loc[:, ['Survived', 'Age']])
# 这里使用loc方法，Sex列的所有满足条件的
train.loc[train.Sex == &quot;male&quot;, ['Sex']], train.loc[train.Sex == &quot;female&quot;, ['Sex']] = 0, 1
# print(Counter(train.Embarked))
train['Embarked'] = train['Embarked'].fillna(&quot;S&quot;)
train.loc[train.Embarked == 'S', ['Embarked']], train.loc[train.Embarked == 'C', ['Embarked']], \
train.loc[train.Embarked == 'Q', ['Embarked']] = 0, 1, 2
for each_name in train.Name:
    length_of_name.append(len(each_name))
    tag = re.search(r, each_name).group(1)
    name.append(tag)
    if tag not in name_tag:
        name_tag[tag] = 0
    name_tag[tag] += 1
tag_map = dict(zip(name_tag.keys(), [i for i in range(1, len(name_tag) + 1)]))
name_tag_digit = []
for each in name:
    name_tag_digit.append(tag_map[each])
train['length of name'] = length_of_name
train['name tag'] = name_tag_digit
# print(train['length of name'])
# columns可以打印标签
# predictors = list(train.columns[2:])
predictors = ['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'length of name', 'name tag', 'Embarked']
#归一化
norm_data =(train[predictors] - train[predictors].min()) / (train[predictors].max() - train[predictors].min())

selector = SelectKBest(f_classif, k=5)
selector.fit(norm_data, train['Survived'])
scores = -np.log10(selector.pvalues_)
plt.figure()
plt.bar(range(len(predictors)), scores)
plt.xticks([i for i in range(9)], predictors, rotation=30)
plt.show()
algorithm = RandomForestClassifier(random_state=1, n_estimators=120, min_samples_split=3, min_samples_leaf=2)
kf = KFold(n_splits=2, shuffle=True, random_state=1)
predictions = []
tag = []
for each_train, test in kf.split(train):
    train_data = (train[predictors].iloc[each_train, :])
    train_target = (train['Survived'].iloc[each_train])
    algorithm.fit(train_data, train_target)
    test_pre = algorithm.predict(train[predictors].iloc[test, :])
    predictions.extend(test_pre)
    tag.extend(test)
count = 0
for key, values in enumerate(tag):
    if predictions[key] != train.Survived[values]:
        count += 1
print(f'rate={1 - (count/len(train))}')
</code></pre>
<pre class="highlight"><code class="">rate=0.8260381593714927
</code></pre>
<ul>
<li>最好的结果还是只有百分之八十多，还需要拜读其他人的作品</li>
</ul>
<pre class="highlight"><code class="">import pandas as pd
import re
import numpy as np
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import f_classif
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import KFold


predictors = ['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'length of name', 'name tag', 'Embarked']
algorithm = RandomForestClassifier(random_state=1, n_estimators=120, min_samples_split=3, min_samples_leaf=2)
train = pd.read_csv(r'C:\Users\asus\Desktop\train.csv')
test_data = pd.read_csv(r'C:\Users\asus\Desktop\test.csv')

def data_processing(train):
    r = re.compile(r', (.*?). ', re.S)
    length_of_name = []
    name_tag = {}
    name = []
    train['Age'] = train['Age'].fillna(train['Age'].median())
    train['Fare'] = train['Fare'].fillna(train['Fare'].median())
    # print(train.describe().loc[:, ['Survived', 'Age']])
    # 这里使用loc方法，Sex列的所有满足条件的
    train.loc[train.Sex == &quot;male&quot;, ['Sex']], train.loc[train.Sex == &quot;female&quot;, ['Sex']] = 0, 1
# print(Counter(train.Embarked))
    train['Embarked'] = train['Embarked'].fillna(&quot;S&quot;)
    train.loc[train.Embarked == 'S', ['Embarked']], train.loc[train.Embarked == 'C', ['Embarked']], \
    train.loc[train.Embarked == 'Q', ['Embarked']] = 0, 1, 2
    for each_name in train.Name:
        length_of_name.append(len(each_name))
        tag = re.search(r, each_name).group(1)
        name.append(tag)
        if tag not in name_tag:
            name_tag[tag] = 0
        name_tag[tag] += 1
    tag_map = dict(zip(name_tag.keys(), [i for i in range(1, len(name_tag) + 1)]))
    name_tag_digit = []
    for each in name:
        name_tag_digit.append(tag_map[each])
    train['length of name'] = length_of_name
    train['name tag'] = name_tag_digit
    return train
    # print(train['length of name'])
    # columns可以打印标签
    # predictors = list(train.columns[2:])

def train_algorithm(train):
    #归一化
    new_algorithm = RandomForestClassifier()
    algorithm = RandomForestClassifier(random_state=1, n_estimators=120, min_samples_split=3, min_samples_leaf=2)
    norm_data =(train[predictors] - train[predictors].min()) / (train[predictors].max() - train[predictors].min())

    selector = SelectKBest(f_classif, k=5)
    selector.fit(norm_data, train['Survived'])
    scores = -np.log10(selector.pvalues_)
    # plt.figure()
    # plt.bar(range(len(predictors)), scores)
    # plt.xticks([i for i in range(9)], predictors, rotation=30)
    # plt.show()
    kf = KFold(n_splits=2, shuffle=True, random_state=1)
    for each_train, test in kf.split(train):
        train_data = (train[predictors].iloc[each_train, :])
        train_target = (train['Survived'].iloc[each_train])
        new_algorithm = algorithm.fit(train_data, train_target)
    return new_algorithm

def test(algorithm, test_data):
    data = test_data[predictors]
    # 检查数据是否有缺失
    # print(data[data.isnull().values == True])
    result = algorithm.predict(test_data[predictors])
    return result

def submit(result):
    file = pd.DataFrame({'PassengerId':test_data['PassengerId'], 'Survived':result})
    file.to_csv(r'C:\Users\asus\Desktop\test_result.csv')


def main():

    new_train_data = data_processing(train)
    new_algorithm = train_algorithm(new_train_data)
    new_test_data = data_processing(test_data)
    result = test(new_algorithm, new_test_data)
    submit(result)

if __name__ == '__main__':
    main()
</code></pre>

      
       
    </div>
</article>



<div class="article_copyright">
    <p><span class="copy-title">文章标题:</span>kaggle项目实战，Titanic analysis</p>
    <p><span class="copy-title">文章字数:</span><span class="post-count">4.1k</span></p>
    <p><span class="copy-title">本文作者:</span><a  title="Miki Zhu">Miki Zhu</a></p>
    <p><span class="copy-title">发布时间:</span>2020-03-19, 10:08:13</p>
    <p><span class="copy-title">最后更新:</span>2020-03-22, 20:33:21</p>
    <span class="copy-title">原始链接:</span><a class="post-url" href="/2020/03/19/kaggle%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/" title="kaggle项目实战，Titanic analysis">http://mikiblog.online/2020/03/19/kaggle%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/</a>
    <p>
        <span class="copy-title">版权声明:</span><i class="fa fa-creative-commons"></i> <a rel="license noopener" href="http://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank" title="CC BY-NC-SA 4.0 International" target = "_blank">"署名-非商用-相同方式共享 4.0"</a> 转载请保留原文链接及作者。
    </p>
</div>



    <div id="vcomment" class="comment"></div> 
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src='//unpkg.com/valine/dist/Valine.min.js'></script>
<script src="https://cdnjs.loli.net/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
    var notify = 'false' == true ? true : false;
    var verify = 'false' == true ? true : false;
    new Valine({
        av: AV,
        el: '#vcomment',
        notify: notify,
        app_id: "3uM9nFQKJdCUOHqwT3LHL5Mt-gzGzoHsz",
        app_key: "3v4CdFJJjh97olrmyHxtdDlt",
        placeholder: "请输入",
        avatar:"mm",
    });
</script>



    





  <section id="comments" class="vcomment">
  </section>

    </div>
    <div class="copyright">
        <p class="footer-entry">©2016-2019 MikiZhu 赣ICP备20004640号</p>
<p class="footer-entry">Built with <a href="https://hexo.io/" target="_blank">Hexo</a> and <a href="https://github.com/yelog/hexo-theme-3-hexo" target="_blank">3-hexo</a> theme</p>

    </div>
    <div class="full-toc">
        <button class="full"><span class="min "></span></button>
<button class="post-toc-menu"><span class="post-toc-menu-icons"></span></button>
<div class="post-toc"><span class="post-toc-title">目录</span>
    <div class="post-toc-content">

    </div>
</div>
<a class="" id="rocket" ></a>

    </div>
</div>
<div class="acParent"></div>

</body>
<script src="/js/jquery.pjax.js?v=1.0.1" ></script>

<script src="/js/script.js?v=1.0.1" ></script>
<script>
    var img_resize = 'default';
    /*作者、标签的自动补全*/
    $(function () {
        $('.search').AutoComplete({
            'data': ['#机器学习','#算法','#博客搭建','#python','#网站推荐','#考研经验',],
            'itemHeight': 20,
            'width': 418
        }).AutoComplete('show');
    })
    function initArticle() {
        /*渲染对应的表格样式*/
        
            $(".post .pjax table").addClass("green_title");
        

        /*渲染打赏样式*/
        

        /*高亮代码块行号*/
        

        /*访问数量*/
        
        $.getScript("//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js");
        

        /*代码高亮，行号对齐*/
        $('.pre-numbering').css('line-height',$('.has-numbering').css('line-height'));

        
        
    }

    /*打赏页面隐藏与展示*/
    

</script>

<!--加入行号的高亮代码块样式-->

<!--自定义样式设置-->
<style>
    
    
    .nav {
        width: 542px;
    }
    .nav.fullscreen {
        margin-left: -542px;
    }
    .nav-left {
        width: 120px;
    }
    
    
    @media screen and (max-width: 1468px) {
        .nav {
            width: 492px;
        }
        .nav.fullscreen {
            margin-left: -492px;
        }
        .nav-left {
            width: 100px;
        }
    }
    
    
    @media screen and (max-width: 1024px) {
        .nav {
            width: 492px;
            margin-left: -492px
        }
        .nav.fullscreen {
            margin-left: 0;
        }
        .nav .hide-list.fullscreen {
            left: 492px
        }
    }
    
    @media screen and (max-width: 426px) {
        .nav {
            width: 100%;
        }
        .nav-left {
            width: 100%;
        }
    }
    
    
    .nav-right .title-list nav a .post-title, .nav-right .title-list #local-search-result a .post-title {
        color: #383636;
    }
    
    
    .nav-right .title-list nav a .post-date, .nav-right .title-list #local-search-result a .post-date {
        color: #5e5e5f;
    }
    
    
    .nav-right nav a.hover, #local-search-result a.hover{
        background-color: #e2e0e0;
    }
    
    

    /*列表样式*/
    
    .post .pjax article .article-entry>ol, .post .pjax article .article-entry>ul, .post .pjax article>ol, .post .pjax article>ul{
        border: #e2dede solid 1px;
        border-radius: 10px;
        padding: 10px 32px 10px 56px;
    }
    .post .pjax article .article-entry li>ol, .post .pjax article .article-entry li>ul,.post .pjax article li>ol, .post .pjax article li>ul{
        padding-top: 5px;
        padding-bottom: 5px;
    }
    .post .pjax article .article-entry>ol>li, .post .pjax article .article-entry>ul>li,.post .pjax article>ol>li, .post .pjax article>ul>li{
        margin-bottom: auto;
        margin-left: auto;
    }
    .post .pjax article .article-entry li>ol>li, .post .pjax article .article-entry li>ul>li,.post .pjax article li>ol>li, .post .pjax article li>ul>li{
        margin-bottom: auto;
        margin-left: auto;
    }
    

    /* 背景图样式 */
    
    


    /*引用块样式*/
    

    /*文章列表背景图*/
    
    .nav-right:before {
        content: ' ';
        display: block;
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        opacity: 0.4;
        background: url("http://q8idlsxje.bkt.clouddn.com/%E6%B5%B7%E6%BB%A9.jpg");
        background-repeat: no-repeat;
        background-position: 50% 0;
        -ms-background-size: cover;
        -o-background-size: cover;
        -moz-background-size: cover;
        -webkit-background-size: cover;
        background-size: cover;
    }
    

    
</style>







  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  <script>
    var GUEST_INFO = ['nick','mail','link'];
    var guest_info = 'nick,mail,link'.split(',').filter(function(item){
        return GUEST_INFO.indexOf(item) > -1
    });
    var notify = 'false' == true;
    var verify = 'false' == true;
    new Valine({
      el: '.vcomment',
      notify: notify,
      verify: verify,
      appId: "3uM9nFQKJdCUOHqwT3LHL5Mt-gzGzoHsz",
      appKey: "3v4CdFJJjh97olrmyHxtdDlt",
      placeholder: "请输入",
      pageSize: '10',
      avatar: 'mm',
      lang: 'zh-cn',
      visitor: 'true'
    });
  </script>

</html>
